var http = require('http');
var util = require('util');
var url = require('url');
var querystring = require('querystring');
var fs = require('fs');
var port = 80;
var ip = '127.0.0.1';
var mysql = require('mysql');
var conn = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '233',
    database:'blogmessage',
    port: 3306
});
conn.connect();
http.createServer(function (request, response) {
	response.statusCode = 200;
	response.statusMessage = "It's not find";
	response.setHeader('Server','private server')
	response.setHeader('Content-Type', 'text/html');
	var ext = request.url.substr(request.url.lastIndexOf(".")+1);
	var exts = {
		html:'text/html',
		js:'application/x-javascript',
		jpg:'image/jpeg',
		css:'text/css'
	}
	if (exts[ext]==null) {
		exts[ext]="text/plain"
	};
	var data ="404 网页错误";
	var setname = "我是留言板";
	var p = url.parse(request.url,true);
	response.setHeader('Content-Type', exts[ext]);
	if (p. pathname == "/index.html") {
		try{
			data = fs.readFileSync('html'+request.url,'utf8');
			data = data.replace(/%title/g,setname);
			// item = fs.readFileSync('data.json','utf8');
			// item = JSON.parse(item);
			
			
			conn.query('select * from message', function(err, rows, fields) {
			    if (err) throw err;
			   var item=rows;
			    var s="";
				for (var i = 0; i <item.length; i++) {
					s+= '<div class="container">';
					s+=	'<h6>'+item[i].author+'<span>'+item[i].time+'</span></h6>';
					s+=	'<p>'+item[i].message+"<a href='del?id="+item[i].id+"'>删除</a></p>";
					s+=	'</div>';
				}

				data = data.replace(/%main/g,s);
				response.end(data);
			});
			// conn.end();
			
		}catch(e){
		}
	}else if (p. pathname == "/form.html") {
		data = fs.readFileSync('.'+request.url,'utf8');
		response.end(data);
	}else if (p.pathname =="/addmessage") {
		var post = ''; 
	    request.on('data', function(chunk){    
	        post += chunk;
	    });
       request.on('end', function(){    
	       s = querystring.parse(post);
	       // mysql 数据库
		    
			
			conn.query('insert into message values(null,"'+s.author+'","'+s.message+'","'+new Date+'")', function(err, rows, fields) {
			    if (err) throw err;
			    console.log(rows);
			});
			// conn.end();
	 
	  //      item = JSON.parse(fs.readFileSync('data.json','utf8'));
			// var date =	new Date;
			// date = JSON.stringify(date);
			// date = date.substr(1,10)
			// item.push({
			// 	text:s.text,
			// 	name:s.name,
			// 	time:date
			// })
			// fs.writeFileSync('data.json',JSON.stringify(item))
			response.writeHead(302,{
				'location':'/index.html'
			});
			response.end(data);
	    });
	}else if (p.pathname =="/del") {
		id = p.query.id;
		// item = JSON.parse(fs.readFileSync('data.json','utf8'));
		// item.splice(del,1);
		// item = JSON.stringify(item);
		// fs.writeFileSync('data.json',item)
		  
			
		conn.query('delete from message where id='+id, function(err, rows, fields) {
		    if (err) throw err;
		    console.log(rows);
		    response.writeHead(302,{
				'location':'/index.html'
			});
			response.end(data);
		});
		// conn.end();
	}
}).listen(port);
console.log('Server running at http://%s:%s/',ip,port);

























// if (ext=='css') {
	// 	response.setHeader('Content-Type', 'text/css');
	// }else if (ext=='.js') {
	// 	response.setHeader('Content-Type', 'application/x-javascript');
	// }else if (ext=='jpg') {
	// 	response.setHeader('Content-Type', 'image/jpeg');
	// }else if (ext=='nts') {
	// 	response.setHeader('Content-Type', 'application/vnd.ms-fontobject');
	// }